<?php
require_once('includes/common.php');
pr($_FILES);
unset($_POST['captcha']);

if(isset($_POST['fname'])){
	 $fname = $_POST['fname'];
}
$fname = explode(" ", $fname);
$letters = "";
foreach ($fname as $value) {
$letters .= substr($value, 0, 1);
}

if(isset($_POST['lname'])){
	 $lname = $_POST['lname'];
}
$lname = explode(" ", $lname);
$letters1 = "";
foreach ($lname as $value) {
$letters1 .= substr($value, 0, 1);
}
function codeGenerator(){
	if(isset($_POST['fname'])){
	 $fname = $_POST['fname'];
}
$fname = explode(" ", $fname);
$letters = "";
foreach ($fname as $value) {
$letters .= substr($value, 0, 1);
}
if(isset($_POST['lname'])){
	 $lname = $_POST['lname'];
}
$lname = explode(" ", $lname);
$letters1 = "";
foreach ($lname as $value) {
$letters1 .= substr($value, 0, 1);
if($_POST['lname'] == ''){
	$letters1 = 'X';
}
	}
$fp = fopen("counterlog.txt", "r");
$count = fread($fp, 1024);
fclose($fp);
$count = $count + 1;
$count = '00'. $count;
$fp = fopen("counterlog.txt", "w");
fwrite($fp, $count);
fclose($fp);
	return strtoupper($letters).strtoupper($letters1).$count;
}
if(isset($_POST['fname'])){
	 $fname = $_POST['fname'];
}
if(isset($_POST['lname'])){
	 $lname = $_POST['lname'];
}
if(isset($_POST['phone'])){
	 $phone = $_POST['phone'];
}
if(isset($_POST['email'])){
	 $email = $_POST['email'];
}
if(isset($_POST['ticket'])){
	 $ticket = $_POST['ticket'];
}
$code = $_POST['code'];
$code = 'OGSFAN'.codeGenerator(); 
$_POST['added_date'] = date('Y-m-d H:i:s');
$added_date = date('Y-m-d H:i:s');
$insertQuery = "INSERT INTO `register` (`fname`,`lname`,`phone`,`email`,`code`,`ticket`,`added_date`) values ('$fname','$lname','$phone','$email','$code','$ticket','$added_date') ";

$insertQuery = trim($insertQuery,', ');
echo $insertQuery;

$result = mysql_query($insertQuery) or die(mysql_error());

// to check how many records we inserted
pr( mysql_affected_rows());

// to get last inserted ID
pr( mysql_insert_id());


$to=$email;

// Your subject
$subject="Your confirmation mail";

// From
$header="from: Comic Con India . \r\n";

// Your message
$message="Welcome to Comic Con India powered by Comic Con India (Pvt.) Ltd. \r\n";
$message.="Your Uniue Code details ‐ Login ID : $email, Uniue Code : $code";

$sentmail = mail($to,$subject,$message,$header);

if(isset($_POST['name']))
{
$name=$_POST['name'];
}
else
{
$name="";
}
//--
if(isset($_POST['phone']))
{
$phone=$_POST['phone'];
}
else
{
$phone="";
}
//--
if(isset($_POST['email']))
{
$email=$_POST['email'];
}
else
{
$email="";
}
if(isset($_POST['age']))
{
$age=$_POST['age'];
}
else
{
$age="0";
}
//--
if(isset($_POST['gender']))
{
$gender=$_POST['gender'];
}
else
{
$gender="n";
}
//--

$id=$_POST['id'];
$added_date=date('Y-m-d H:i:s');
//=================================

header('location:registation.php');

?>